//
//  VCOfMy.m
//  ProjectA_ManualGuest
//
//  Created by dingcong on 16/6/28.
//  Copyright © 2016年 丁聪. All rights reserved.
//

#import "VCOfMy.h"
#import "CellOfUserLogin.h"
#import "CellForHeader.h"
#import "UIImageView+WebCache.h"
#import "CollectionVC.h"
#import "SandBoxStore.h"
#import "Settings.h"
@interface VCOfMy ()

@property(nonatomic, assign)CGFloat fileSize;

@property(nonatomic, retain)NSString *strCashPath;

@property(nonatomic, retain)SandBoxStore *sand;

@property(nonatomic, retain)UILabel *LableOfcachSize;

@property(nonatomic, retain)UIButton *buttonOfNight;



@end

@implementation VCOfMy
- (void)dealloc{
    
    [_strCashPath release];
    [_sand release];
    [super dealloc];
    
    
}

- (instancetype)init{
        if (self = [super init])
        {
            self.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的" image:[UIImage imageNamed:@"5.png"] tag:200];
            
            self.view.backgroundColor = [UIColor colorWithRed:248.0 / 256.0f green:239.0 / 256.0f blue:239.0 / 256.0f alpha:1];
            
            
        }
        return self;
    
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [self.navigationItem setTitle:@"我的"];
    
    
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        
        self.view.backgroundColor = [[Settings shareSettings]color:PinkToGray];
        
        
    }];
    
    
    self.buttonOfNight = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.buttonOfNight.frame = CGRectMake(SCREEN_WIDTH / 1.5, SCREEN_HEIGHT / 3, 100, 30);
    self.buttonOfNight.titleLabel.font = [UIFont systemFontOfSize:14];
    [self.buttonOfNight addTarget:self action:@selector(ChangeColor:) forControlEvents:UIControlEventTouchUpInside];
    [self.buttonOfNight setTitle:@"点击选择模式" forState:UIControlStateNormal];
    [self.buttonOfNight setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.tableView addSubview:_buttonOfNight];
    self.buttonOfNight.layer.cornerRadius = 14;
    self.buttonOfNight.backgroundColor = [UIColor whiteColor];
    
    
    [self.tableView registerClass:[CellForHeader class] forCellReuseIdentifier:@"Header"];
    [self.tableView registerClass:[CellOfUserLogin class] forCellReuseIdentifier:@"pool"];
}

- (void)ChangeColor:(UIButton *)change{
    
    [Settings shareSettings].isNight = ![Settings shareSettings].isNight;
    [[NSNotificationCenter defaultCenter]postNotificationName:@"changeBackgroundColor" object:nil userInfo:nil];
    if ([Settings shareSettings].isNight)
    {
        [change setTitle:@"开启日间模式" forState:UIControlStateNormal];

        change.backgroundColor = [UIColor blackColor];
        [change setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }
    else
    {
        [change setTitle:@"开启夜间模式" forState:UIControlStateNormal];

        change.backgroundColor = [UIColor whiteColor];
        [change setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 50;
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return 2;
    
    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (section == 0)
    {
        return 2;
    }
    else
    {
        
        return 5;
        
    }
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CellOfUserLogin *cell =  [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    cell.backgroundColor = [UIColor clearColor];
    if (indexPath.row == 0 && indexPath.section == 0)
    {
        cell.label.text = @"我的收藏";
    }
    else if (indexPath.row == 1 && indexPath.section == 0)
    {
        cell.label.text = @"手工圈";
    }
    else if (indexPath.row == 0 && indexPath.section == 1)
    {
        
        cell.label.text = @"清除缓存";
        
        _strCashPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
        NSLog(@"%@", _strCashPath);
        self.fileSize = [[SandBoxStore sharedSandBoxStore]folderSizeAtPath:_strCashPath];
        NSLog(@"%.2f", self.fileSize);
        _LableOfcachSize = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 1.3, 10, 70, 30)];
        [cell addSubview:_LableOfcachSize];
        _LableOfcachSize.text = [[NSString stringWithFormat:@"%.2f", self.fileSize]stringByAppendingString:@" M"];
        _LableOfcachSize.textColor = [UIColor redColor];
        
    }
    else if(indexPath.row == 1 && indexPath.section == 1)
    {
        cell.label.text = @"夜间模式";
        
    }
    else if (indexPath.row == 2 && indexPath.section == 1)
    {
        cell.label.text = @"帮助中心";
    }
    
    else if (indexPath.row == 3 && indexPath.section == 1)
    {
        cell.label.text = @"意见反馈";
    }
    else if (indexPath.row == 4 && indexPath.section == 1)
    {
        cell.label.text = @"关于我们";
    }

    cell.Rightimage.image = [UIImage imageNamed:@"zuo"];
    
    return cell;
    
}

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
//    self.LableOfcachSize.text = nil;
//
//    [self.tableView reloadData];
    
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UILabel *lbl = [[UILabel alloc] init];
    lbl.textAlignment = NSTextAlignmentCenter;
    lbl.backgroundColor = [UIColor clearColor];
    lbl.textColor = [UIColor grayColor];
    if (section == 0)
    {
        lbl.text = @"收藏";
        
        return lbl;
       
        
    }
   
    else{
        lbl.text = @"个人设置";
        
        return lbl;
        
        
    }
    
    
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath{
    
    if (indexPath.section == 0 && indexPath.row == 0)
    {
        CollectionVC *collection = [[CollectionVC alloc]init];
        
        
        [self.navigationController pushViewController:collection animated:YES];
        
    }
    else if(indexPath.section == 1 && indexPath.row == 0)
    {
        
       
        
        
        
    
        UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示"
                                                                       message:@"清除缓存成功😄!!!"
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action)
        {
            [[SandBoxStore sharedSandBoxStore]clearCachFile];
            NSString *cachpath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
            CGFloat l = [[SandBoxStore sharedSandBoxStore]folderSizeAtPath:cachpath];
            
           
            
            _LableOfcachSize.text = [[NSString stringWithFormat:@"%.2f", l]stringByAppendingString:@" M"];
                                                              
                                                              
                                                              
                                                              
        }];
        
        [alert addAction:defaultAction];
        [self presentViewController:alert animated:YES completion:nil];
        
    }
    
    
    
    
    
}


@end
